package org.catdroid.bcnappsjam.barcelonaparticipa.Adapter;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import org.catdroid.bcnappsjam.barcelonaparticipa.R;
import org.catdroid.bcnappsjam.barcelonaparticipa.Objectes.Replies;
import org.catdroid.bcnappsjam.barcelonaparticipa.Objectes.Reply;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AdapterListReplise extends BaseAdapter {
	private Replies dades;
	private double vots;
	private Context context;

	public AdapterListReplise(Replies respostes, int vots, Context activity) {
		dades = respostes;
		this.vots = vots;
		context = activity;
	}

	public int getCount() {
		return dades.size();
	}

	public Object getItem(int arg0) {
		return dades.get(arg0);
	}

	public long getItemId(int arg0) {
		return arg0;
	}

	public View getView(int arg0, View arg1, ViewGroup arg2) {
		View v = arg1;
		ViewHolder holder = null;

		if (arg1 == null) {

			LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.item_respostes, null);
			holder = new ViewHolder();
			holder.progress = (ProgressBar) v.findViewById(R.id.prgVotacions);
			holder.tantpercent = (TextView) v.findViewById(R.id.txtTanParcent);
			holder.tantvots = (TextView) v.findViewById(R.id.txtTanVots);
			holder.titulo = (TextView) v.findViewById(R.id.txtNomResposta);
			v.setTag(holder);

		} else {
			holder = (ViewHolder) v.getTag();
		}

		Reply item = (Reply) getItem(arg0);

		holder.progress.setMax((int) vots);
		holder.progress.setProgress(item.getNumberOfVotes());

		holder.titulo.setText(item.getText());

		double tantpercent = item.getNumberOfVotes() / vots;

		NumberFormat numberFormatter = NumberFormat.getNumberInstance(Locale.getDefault());
		DecimalFormat decimalFormatter = new DecimalFormat("#########0.00");
		String oubleNumOut = decimalFormatter.format(tantpercent * 100);

		holder.tantpercent.setText(oubleNumOut + "%");
		holder.tantvots.setText(numberFormatter.format(item.getNumberOfVotes()) + " vots");

		Log.i("valor", item.getNumberOfVotes() + "");
		Log.i("valor tant", tantpercent + "");
		return v;
	}

	static class ViewHolder {
		ProgressBar progress;
		TextView titulo;
		TextView tantpercent;
		TextView tantvots;
	}
}
